package com.gmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmu.domain.ResResult;
import com.gmu.domain.vo.PageVo;
import com.gmu.mapper.BeSampleUnitsActivitiesMapper;
import com.gmu.mapper.TblActivitiesAddressMapper;
import com.gmu.mapper.TblUnitsMapper;
import com.gmu.pojo.BeSampleUnitsActivities;
import com.gmu.pojo.TblActivities;
import com.gmu.mapper.TblActivitiesMapper;
import com.gmu.pojo.TblActivitiesAddress;
import com.gmu.pojo.TblUnits;
import com.gmu.service.TblActivitiesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmu.service.TblUnitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 测核酸活动 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Service
public class TblActivitiesServiceImpl extends ServiceImpl<TblActivitiesMapper, TblActivities> implements TblActivitiesService {

    @Autowired
    private TblActivitiesMapper tblActivitiesMapper;

    @Autowired
    private TblUnitsService tblUnitsService;

    @Override
    public ResResult<TblActivities> commitActivities(TblActivities tblActivities) {

        tblActivities.setStatus(0);//活动设为未举行状态

        if ((tblActivitiesMapper.insert(tblActivities)) < 1){
            return new ResResult<>(500,"提交失败");
        }

        return new ResResult<>(200,"提交成功");
    }

    @Override
    public ResResult<?> showReservationActivities(Integer unitsId) {

        Integer parentUnitsId = recursionObtainName(unitsId);
        List<TblActivities> tblActivities = tblActivitiesMapper.showReservationActivities(parentUnitsId);
        return new ResResult<>(200,tblActivities);
    }

    @Override
    public PageVo<?> getActivityRecordList(Integer pageNum, Integer pageSize, TblActivities tblActivities, String beginTime, String endTime) {
        Page<TblActivities> page = new Page<>(pageNum,pageSize);
        IPage<TblActivities> iPage = tblActivitiesMapper.getActivityRecordList(page,tblActivities,beginTime,endTime);
        PageVo<TblActivities> pageVo = new PageVo<>();
        pageVo.setCurrent((int) iPage.getCurrent());
        pageVo.setSize((int) iPage.getSize());
        pageVo.setList(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        pageVo.setCode(200);
        return pageVo;
    }

    @Override
    public ResResult<?> deleteRecord(Integer[] activitiesIds) {
        int result = tblActivitiesMapper.deleteRecord(activitiesIds);
        return result > 0 ? new ResResult<>(200,"删除成功"):new ResResult<>(200,"删除失败");
    }

    @Override
    public List<TblActivities> exportRecord(TblActivities tblActivities, String beginTime, String endTime) {
        List<TblActivities> list = tblActivitiesMapper.getRecordList(tblActivities,beginTime,endTime);
        for (TblActivities tblActivity:list) {
            if (tblActivity.getStatus()==0) {
                tblActivity.setSta("未举行");
            } else if (tblActivity.getStatus()==1) {
                tblActivity.setSta("举行中");
            } else {
                tblActivity.setSta("结束");
            }
        }
        return list;
    }

    @Override
    public ResResult<?> getAllActivitiesByUnitsId(Integer unitsId) {
        Integer parentUnitsId = recursionObtainName(unitsId);
        List<TblActivities> tblActivities = tblActivitiesMapper.getAllActivitiesByUnitsId(parentUnitsId);
        return new ResResult<>(200,tblActivities);
    }

    @Override
    public ResResult<?> getAllActivitiesHeld(Integer unitsId) {
        Integer parentUnitsId = recursionObtainName(unitsId);
        List<TblActivities> tblActivities = tblActivitiesMapper.getAllActivitiesHeld(parentUnitsId);
        return new ResResult<>(200,tblActivities);
    }

    private Integer recursionObtainName(Integer unitsId) {
        TblUnits tblUnits = tblUnitsService.getOne(new QueryWrapper<TblUnits>().eq("units_id", unitsId));
        if (tblUnits.getParentId() < 0) {
            return tblUnits.getUnitsId();
        }
        return recursionObtainName(tblUnits.getParentId());
    }
}
